<?php

class Common_DataTable_Summary_Sum extends Common_DataTable_Summary_Calculated {

    private $arr_sums = array();
    private $arr_group_sums = array();
    
    public function __construct() {
        parent::__construct();
    }
    
    public function initialise($arr_columns, $arr_groups = null) {
        
        parent::initialise($arr_columns, $arr_groups);
        
        $arr_column_keys = $this->getApplicableColumnKeys();
        
        $this->arr_sums = array();
        foreach($arr_column_keys as $str_column_key) {
            $this->arr_sums[$str_column_key] = 0;
        }
        
        foreach($arr_groups as $str_group_key) {
            $this->arr_group_sums[$str_group_key] = array();
            foreach($arr_column_keys as $str_column_key) {
                $this->arr_group_sums[$str_group_key][$str_column_key] = 0;
            }
        }
        
    }
    
    public function reviewRow($arr_row, $mix_grouping_keys = null) {
        
        $str_group_key = null;
        
        $arr_grouping_keys = array();
        if(!is_null($mix_grouping_keys)) {
            $arr_grouping_keys = (array) $mix_grouping_keys;
        }
        
        $arr_keys = array();
        foreach($arr_grouping_keys as $str_grouping_index => $str_grouping_key) {

            if(is_integer($str_grouping_index)) {
                $str_grouping_index = $str_grouping_key;
            }

            $mix_group_index = isset($arr_row[$str_grouping_index]) ? $arr_row[$str_grouping_index] : null;
            $mix_group_value = isset($arr_row[$str_grouping_key]) ? $arr_row[$str_grouping_key] : null;

            $arr_keys[] = $mix_group_index;

            $str_group_key = implode('|', $arr_keys);
        }
        
        
            
        $arr_column_keys = $this->getApplicableColumnKeys();
        
        foreach($arr_column_keys as $str_column_key) {
            
            if(isset($arr_row[$str_column_key])) {
                $mix_value = $arr_row[$str_column_key];
                
                $this->arr_sums[$str_column_key] += $mix_value;
                
                if(!is_null($str_group_key)) {
                    $this->arr_group_sums[$str_group_key][$str_column_key] += $mix_value;
                }
            }
            
        }
        
        
    }
    
    protected function _getData() {
        return $this->arr_sums;
    }
    
    protected function _getDataForGroup($str_group_key) {
        return $this->arr_group_sums[$str_group_key];
    }

}
